//  公用请求方法
function Ajax(url, method, data, success){
  wx.showLoading({
    title: '加载中...',
  })

  //正式域名
  var ServerUrl = 'https://xcx.folomeet.com/';

  // 本地取存储的 Token
  var Token = wx.getStorageSync('Token');
  
  if (Token != "" && Token != null) {
    var header = { 
      'Accept': 'application/json', 
      'content-type':'application/json',
      'Authorization': Token 
    };
  } else {
    var header = { 'content-type': 'application/x-www-form-urlencoded' };
  }
  wx.request({
    url: ServerUrl + url,
    method: method,
    data: data,
    header: header,
    success: (res) => {
      wx.hideLoading();
      var data = res.data;
      // 返回参数 ‘C’
      // 200 正常
      // 201 Token 错误
      if (data.c != 200){
        wx.removeStorageSync('Token');
        wx.removeStorageSync('UserInfor');
      }
      if (res.header.Authorization != '' && res.header.Authorization != null){
        wx.setStorageSync('Token', res.header.Authorization);
      }
      res['statusCode'] === 200 ? success(data) : that.fail();
    },
    fail: function(res){
      wx.showToast({
        title: '请求超时',
        icon: 'loading',
        duration: 2000
      })
      wx.hideLoading();
    }
  });
}

module.exports = {
  Ajax: Ajax
}  